package com.decoration.basic.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.StringUtils;
import com.decoration.basic.BasicController;
import com.decoration.basic.entity.*;
import com.decoration.basic.findCondition.DecFlowFindCondition;
import com.decoration.basic.service.DecFlowService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class CtctionFlowController extends BasicController {

	private static Logger log = LoggerFactory.getLogger(CtctionFlowController.class);
	@Resource
	private DecFlowService decFlowService;

	@Resource
	private DecUserService decUserService;

	//获取模板流程列表，包含其下阶段等信息
	@RequestMapping(value = "/flowTemple/lists", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getDecFlowTempleLists(JsonResult jsonResult, @RequestBody DecFlowFindCondition findCondition) {
		log.info("获取模板流程列表");
		//设置页面参数
		setPagerInfo(findCondition);
		List<DecFlowFindCondition> list = decFlowService.findAllTemple(findCondition);

//		JsonResult jsonResult = new JsonResult();
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(list == null ? 0:list.size());
		jsonResult.setData(list);
		return jsonResult;
	}

	//获取模板流程详情
	@RequestMapping(value = "/flow/{flowId}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getDecFlowByFlowId(JsonResult jsonResult, @PathVariable String flowId) {


		DecFlowFindCondition decFlowInf = decFlowService.findById(flowId);

		if (decFlowInf == null || decFlowInf.getFlowId().equals("")){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取模板信息失败");
			return jsonResult;
		}

		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取模板信息成功");
		jsonResult.setCount(1);
		jsonResult.setData(decFlowInf);
		return jsonResult;
	}

	//获取新增项目中流程模板下拉框列表
	@RequestMapping(value = "/flow/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getDecFlowList(JsonResult jsonResult) {

		List<DecPhaseBase> list = decFlowService.findTempleList();

		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取流程列表成功");
		jsonResult.setCount(list.size());
		jsonResult.setData(list);
		log.info("响应信息为：["+JSONUtil.toJsonStr(list)+"]");
		return jsonResult;
	}

	/**
	 * 新增模板流程
	 * @param jsonResult
	 * @param decFlowFindCondition
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/flow/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addDecFlow(JsonResult jsonResult, @RequestBody DecFlowFindCondition decFlowFindCondition,
								 HttpServletRequest request) {

		//校验用户信息
		String userid = (String) request.getSession().getAttribute(Global.SESSION_USER_ID);
		DecorationUser user = decUserService.findById(userid);

		if (user ==null || user.getPerType() != 0){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("用户无权限");
			return jsonResult;
		}
		decFlowFindCondition.setPerId(user.getUpdateId());
		/*if (null == decFlowFindCondition ||  StringUtils.isEmpty(decFlowFindCondition.getFlowId()) ) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要添加的项目流程信息为空,请重试!");
			return jsonResult;
		}*/

		int res = decFlowService.insert(decFlowFindCondition);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目模板流程添加失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("添加成功");
		return jsonResult;
	}

	//更新模板流程
	@RequestMapping(value = "/flow/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateDecFlow(JsonResult jsonResult, @RequestBody DecFlowFindCondition decFlowFindCondition) {
		if (null == decFlowFindCondition || StringUtils.isEmpty(decFlowFindCondition.getFlowId())) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要更新的项目模板流程信息为空,请重试!");
			return jsonResult;
		}

		int res = decFlowService.updateById(decFlowFindCondition);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目模板流程更新失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("项目模板流程更新成功");
		return jsonResult;
	}

	/**
	 * 删除模板流程
	 * @param jsonResult
	 * @param decFlowInf
	 * @return
	 */
	@RequestMapping(value = "/flow/del", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delDecFlow(JsonResult jsonResult, @RequestBody DecFlowInf decFlowInf) {

		if ( null == decFlowInf.getFlowId()) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("输入需要删除的模板流程id为空,请重试!");
			return jsonResult;
		}

		int res = decFlowService.deleteById(decFlowInf.getFlowId());
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("项目模板流程删除失败,请联系管理员!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("项目模板流程删除成功");
		jsonResult.setCount(res);
		return jsonResult;
	}

	//获取师傅名称下拉框列表
	@RequestMapping(value = "/master/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getDecUserList(JsonResult jsonResult) {
		List<DecPhaseBase> list = decFlowService.findMasterTempleList();
		if (list == null || list.size() == 0){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取师傅列表失败");
			jsonResult.setData(list);
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取师傅列表成功");
		jsonResult.setCount(list.size());
		jsonResult.setData(list);
		return jsonResult;
	}

	//获取师傅类型下拉框列表
	@RequestMapping(value = "/masterType/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getMasterTypeList(JsonResult jsonResult) {

		List<DecPhaseBase> list = decFlowService.findMasterTypeList();
		if (list == null || list.size() == 0){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取师傅类型列表失败");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取师傅类型列表成功");
		jsonResult.setCount(list.size());
		jsonResult.setData(list);
		return jsonResult;
	}

	//获取阶段下拉框列表
	@RequestMapping(value = "/phase/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getDecPhaseList(JsonResult jsonResult) {
		List<DecPhaseBase> list = decFlowService.findPhaseTempleList();
		if (list == null || list.size() == 0){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("获取阶段列表失败");
			jsonResult.setData(list);
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("获取阶段列表成功");
		jsonResult.setCount(list.size());
		jsonResult.setData(list);
		return jsonResult;
	}

	/**
	 * 获取关联流程信息，包含其下阶段等信息
	 * @param jsonResult
	 * @param findCondition
	 * @return
	 */
	@RequestMapping(value = "/decflow/lists", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getDecFlowById(JsonResult jsonResult, @RequestBody DecFlowFindCondition findCondition) {
		setPagerInfo(findCondition);

        DecFlowFindCondition list = null;
        try {
            list = decFlowService.findDecFlowbyId(findCondition);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("需要添加的项目流程信息异常,请联系管理员!");
            return jsonResult;
        }

        if (null == list ) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要添加的项目流程信息为空,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(1);
		jsonResult.setData(list);
		return jsonResult;
	}

	/**
	 * 新增/更新 关联流程 -- 阶段对应的师傅信息，阶段信息
	 * @param jsonResult
	 * @param findCondition
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ctcflow/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addCtcDecFlow(JsonResult jsonResult, @RequestBody DecFlowFindCondition findCondition,
									HttpServletRequest request) {

		//校验用户权限
		String userid = (String) request.getSession().getAttribute(Global.SESSION_USER_ID);
		DecorationUser user = decUserService.findById(userid);

		if (user ==null || user.getPerType() != 0){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("用户未登录/用户无权限");
			return jsonResult;
		}

		//2.完善关联流程内容
		Boolean list = decFlowService.addCtcFlow(findCondition);

		if (list){
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("完善流程信息失败");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("完善流程信息成功");
		return jsonResult;
	}

}
